package com.tzbank.wisdom.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.service.TzbCardRegisterService;
import com.tzbank.wisdom.utils.BusinessConstant;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

/**
 * 本行卡注册及注册前置检查
 *
 * @author gaof
 */
@Service
@Slf4j
public class TzbCardRegisterServiceImpl implements TzbCardRegisterService {

    @Override
    public Map<String, Object> tzbCardReg(Map<String, String> reqBody) {
        log.info("入参【{}】", JSONObject.fromObject(reqBody));
        Map<String, Object> checkMap = tzbRegisterCheck(reqBody);
        String code = (String) checkMap.get("code");
        if (code.equals("200")) {
            Map<String, Object> dataMap = (Map<String, Object>) checkMap.get("data");
            Map<String, Object> registerInputMap = new HashMap<String, Object>();
            Map<String, Object> registerReqSysHead = RequestMap.getReqSysHead(TransCodeEnum.TZB_REGISTER.getServiceCode(), TransCodeEnum.TZB_REGISTER.getServiceSence(), "", "0110", "");
            registerInputMap.put("sysHead", registerReqSysHead);
            registerInputMap.put("clientOpenFlag", dataMap.get("clientOpenFlag"));//客户开通标志0
            registerInputMap.put("acctNo", dataMap.get("acctNo"));//注册卡号6224271190394088-101
            registerInputMap.put("bankSerNo", dataMap.get("bankSerNo"));//机构序号8
            registerInputMap.put("acctType", dataMap.get("acctType"));//账户类型PSAV
            registerInputMap.put("acctName", dataMap.get("acctName"));//账户名称李建辉
            registerInputMap.put("clientName", dataMap.get("clientName"));//客户名称李建辉
            registerInputMap.put("gender", dataMap.get("gender"));//性别M
            registerInputMap.put("globalId", dataMap.get("globalId"));//证件号码454566196306084419
            registerInputMap.put("globalType", dataMap.get("globalType"));//证件类型P00
            registerInputMap.put("birthDate", dataMap.get("birthDate"));//19630608
            registerInputMap.put("mobileNo", reqBody.get("mobileNo"));//移动电话15201240619
            registerInputMap.put("homeAddress", "111");//家庭住址
            registerInputMap.put("postcode", "111");//邮政编码
            registerInputMap.put("tranPassword", reqBody.get("tranPassword"));//交易密码
            registerInputMap.put("password", reqBody.get("password"));//密码
            registerInputMap.put("passwordConf", reqBody.get("passwordConf"));//确认密码
            registerInputMap.put("ccy", dataMap.get("ccy"));//币种
            registerInputMap.put("bankNo", reqBody.get("bankNo"));//银行代码
            Map<String, Object> result = EsbConsumerUtil.clientRequest(registerInputMap);
            if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
                return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                        result.get("data"));
            }
            return ResultUtil.resultMap("本行卡注册成功 ", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
        } else {
            return checkMap;
        }
    }

    private Map<String, Object> tzbRegisterCheck(Map<String, String> map) {

        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead1 = RequestMap.getReqSysHead(TransCodeEnum.TZB_REGISTER_QUERY.getServiceCode(), TransCodeEnum.TZB_REGISTER_QUERY.getServiceSence(), "", "0110", "");
        inputMap.put("sysHead", reqSysHead1);
        inputMap.put("cardNo", map.get("cardNo"));//注册卡号6224271190394088
        inputMap.put("clientName", map.get("clientName"));//客户名称李建辉
        inputMap.put("mobileNo", map.get("mobileNo"));//移动号码15201240619
        inputMap.put("globalId", map.get("globalId"));//证件号码454566196306084419
        inputMap.put("oppenAcctType", map.get("oppenAcctType"));//开户方式0
        inputMap.put("msgVerCode", "111");//短信验证码reqBody.get("msgVerCode")
        inputMap.put("agentSeqNo", "111");//中间业务平台流水号
        inputMap.put("othBankName", "111");//对方行名
        inputMap.put("checkBranch", "111");//核查机构
        inputMap.put("openBranch", "111");//开户机构
        inputMap.put("bankNo", map.get("bankNo"));//银行代码04590501
        inputMap.put("sendDate", new Date() + "");
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
        String code = result.get("code").toString();
        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            if (code.equals(BusinessConstant.REGITER)) {
                return ResultUtil.resultMap(result.get("msg").toString(), CodeUtil.CODE_10015.getCode(),
                        result.get("data"));
            } else {
                return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                        result.get("data"));
            }
        }
        return ResultUtil.resultMap("本行卡注册前置查询成功 ", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }
}


